package org.example;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class BeifutingSkinDetection {
    // 旷视API
    private static final String FACEPLUSPLUS_API_KEY = "3UznLENkCCGHI1VBxtqlfzxy2Rg6zv6J";
    private static final String FACEPLUSPLUS_API_SECRET = "g3iL_gem_67PeKi96nVUGNinjDYeZGqn";
    private static final String FACEPLUSPLUS_API_URL = "https://api-cn.faceplusplus.com/facepp/v1/skinanalyze_advanced";

    // 扣子智能体API
    private static final String KOUZI_AGENT_API_KEY = "pat_cYF2Ud8r0MZeUayT1jSjU7gpuIX7PjgGyCFikTQa46HCO12y4BnQrYGUFFMZOwLa";
    private static final String KOUZI_AGENT_API_URL = "https://www.coze.cn/space/7494493821301112884/bot/7496420663788961827";

    /**
     * 调用旷视皮肤分析 API
     * @param imageFile 待检测的图片文件
     * @return 旷视 API 的分析结果字符串
     * @throws IOException 网络请求或文件操作异常
     */
    public static String callFacePlusPlusAPI(File imageFile) throws IOException {
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(FACEPLUSPLUS_API_URL);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("api_key", FACEPLUSPLUS_API_KEY);
        builder.addTextBody("api_secret", FACEPLUSPLUS_API_SECRET);
        builder.addBinaryBody("image_file", imageFile, ContentType.APPLICATION_OCTET_STREAM, imageFile.getName());

        HttpEntity multipart = builder.build();
        httpPost.setEntity(multipart);

        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity responseEntity = response.getEntity();

        return EntityUtils.toString(responseEntity);
    }

    /**
     * 调用扣子智能体进行分析
     * @param analysisResult 旷视 API 的分析结果字符串
     * @return 扣子智能体的分析结果字符串
     * @throws IOException 网络请求异常
     */
    public static String callKouziAgent(String analysisResult) throws IOException {
        URL url = new URL(KOUZI_AGENT_API_URL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Authorization", "Bearer " + KOUZI_AGENT_API_KEY);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);

        try (java.io.OutputStream os = connection.getOutputStream()) {
            byte[] input = analysisResult.getBytes("utf-8");
            os.write(input, 0, input.length);
        }

        try (java.io.BufferedReader br = new java.io.BufferedReader(
                new java.io.InputStreamReader(connection.getInputStream(), "utf-8"))) {
            StringBuilder response = new StringBuilder();
            String responseLine;
            while ((responseLine = br.readLine()) != null) {
                response.append(responseLine.trim());
            }
            return response.toString();
        }

    }

    public static void main(String[] args) {
        //图片路径
        File imageFile = new File("C:\\Users\\Administrator\\Pictures\\mz\\demo-pic-skinAnalysis-1.jpg");
        try {
            // 调用旷视 API 进行皮肤分析
            String facePlusPlusResult = callFacePlusPlusAPI(imageFile);
            System.out.println("旷视 API 分析结果：" + facePlusPlusResult);

            // 将旷视 API 的分析结果传递给扣子智能体进行分析
            String kouziAgentResult = callKouziAgent(facePlusPlusResult);
            System.out.println("扣子智能体分析结果：" + kouziAgentResult);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}    